﻿using System;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using QSBusinessLayerCSharp;
using ValidationFramework;

namespace QSWPFApplicationCSharp
{
	/// <summary>
	/// Interaction logic for MultiLanguageWindow.xaml
	/// </summary>
	public partial class MultiLanguageWindow : Window
	{
		readonly Registration registration;
		public MultiLanguageWindow()
		{
			InitializeComponent();
			registration = new Registration();
			DataContext = registration;
			Loaded += MultiLanguageWindow_Loaded;
		}

		void MultiLanguageWindow_Loaded(object sender, RoutedEventArgs e)
		{
			cultureListBox.SelectedIndex = 0;
		}
		
		private void validateButton_Click(object sender, RoutedEventArgs e)
		{

			var manager = new PropertyValidationManager(registration);
			manager.ValidateAll();
			errorTreeView.Items.Clear();
			foreach (var result in manager.ValidatorResultsInError)
			{
				var node = new TreeViewItem {Header = result.ErrorMessage};
			    node.Items.Add(String.Format("RuleInterpretation = '{0}'", result.Rule.RuleInterpretation));
				node.Items.Add(String.Format("Rule = '{0}'", result.Rule.GetType()));
				errorTreeView.Items.Add(node);
			}
		}

		private void cultureListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			var listBoxItem = (ListBoxItem) cultureListBox.SelectedItem;
			Thread.CurrentThread.CurrentCulture = new CultureInfo((string)listBoxItem.Content);
		}
	}
}
